package com.seguriapp.activities;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.ImageButton;

import com.seguriapp.R;
import com.seguriapp.controllers.SignalController;
import com.seguriapp.entities.Point;
import com.seguriapp.entities.SignalEntity;
import com.seguriapp.social.TwitterApp;
import com.seguriapp.util.AppPrefences;
import com.seguriapp.util.GPS;

/**
 * @author pjohnson
 * 
 */
public class SignalActivity extends Activity {

	private SignalActivity mContext;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_signal);
		final AppPrefences msp = new AppPrefences(this);
		mContext = this;

		final CheckBox chkGps = (CheckBox) findViewById(R.id.checkGps);
		final CheckBox chkFacebook = (CheckBox) findViewById(R.id.checkFacebook);
		final CheckBox chkTwitter = (CheckBox) findViewById(R.id.checkTwitter);

		chkGps.setChecked(msp.isAlwaysGps());
		chkFacebook.setChecked(msp.isAlwaysFb());
		chkTwitter.setChecked(msp.isAlwaysTw());

		final ImageButton btnSignal = (ImageButton) findViewById(R.id.signalButton);
		btnSignal.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(final View arg0) {
				// final boolean isGpsEnabled = chkGps.isChecked();
				final boolean isFacebookEnabled = chkFacebook.isChecked();
				final boolean isTwitterEnabled = chkTwitter.isChecked();

				final GPS gps = new GPS();
				final Location lcte = gps.getCurFix(mContext);
				final Point point = new Point();
				point.mLatitude = lcte.getLatitude();
				point.mLongitude = lcte.getLongitude();

				gps.removeUpdates(gps);

				if (isTwitterEnabled) {
					final TwitterApp twitter = new TwitterApp(mContext,
							"Estoy en una emergencia!!",
							TwitterApp.IMPLICIT_MODE);
					twitter.shareInTwitter("Estoy en una emergencia!! http://maps.google.com/?q="
							+ point.mLatitude + "," + point.mLongitude);
				}

				if (isFacebookEnabled) {
//					final FacebookManager fbManager = new FacebookManager(
//							mFacebook, mContext);
//					fbManager.shareInFacebook(mContext,
//							"Estoy en una emergencia!! http://maps.google.com/?q="
//									+ point.mLatitude + "," + point.mLongitude);
				}

				final AppPrefences msp = new AppPrefences(mContext);

				final SignalEntity signal = new SignalEntity();
				signal.setLastName(msp.getUserLastName());
				signal.setName(msp.getUserName());
				signal.setDni(msp.getUserDni());
				signal.setPhone(msp.getUserPhone());

				SimpleDateFormat sdf = new SimpleDateFormat(
						"ddMMyyyy hh:mm:ss", Locale.US);
				signal.setDate(sdf.format(new Date()));

				signal.setLatitude(lcte.getLatitude());
				signal.setLongitude(lcte.getLongitude());

				Geocoder myLocation = new Geocoder(getApplicationContext(),
						Locale.getDefault());
				try {
					List<Address> myList = myLocation.getFromLocation(
							lcte.getLatitude(), lcte.getLongitude(), 1);
					signal.setPostalCode(myList.get(0).getAddressLine(1));
				} catch (IOException e) {
					Log.e("Error en el zip code", e.getMessage());
					//TODO: sacar un postal code x defecto (San miguel)
					signal.setPostalCode("Lima 15088");
				}

				final SignalController sController = new SignalController(
						mContext);
				sController.sendSignal(signal);

			}
		});
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onActivityResult(int, int,
	 * android.content.Intent)
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
//		mFacebook.authorizeCallback(requestCode, resultCode, data);
	}

}
